package com.alipay.api;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayParser;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.AlipayUploadRequest;
import com.alipay.api.SignItem;
import com.alipay.api.internal.parser.json.ObjectJsonParser;
import com.alipay.api.internal.parser.xml.ObjectXmlParser;
import com.alipay.api.internal.util.AlipayHashMap;
import com.alipay.api.internal.util.AlipayLogger;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.internal.util.AlipayUtils;
import com.alipay.api.internal.util.RequestParametersHolder;
import com.alipay.api.internal.util.StringUtils;
import com.alipay.api.internal.util.WebUtils;
import java.io.IOException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

public class DefaultAlipayClient implements AlipayClient {

   private String serverUrl;
   private String appId;
   private String privateKey;
   private String prodCode;
   private String format;
   private String sign_type;
   private String alipayPublicKey;
   private String charset;
   private int connectTimeout;
   private int readTimeout;


   public DefaultAlipayClient(String serverUrl, String appId, String privateKey) {
      this.format = "json";
      this.sign_type = "RSA";
      this.connectTimeout = 3000;
      this.readTimeout = 15000;
      this.serverUrl = serverUrl;
      this.appId = appId;
      this.privateKey = privateKey;
      this.alipayPublicKey = null;
   }

   public DefaultAlipayClient(String serverUrl, String appId, String privateKey, String format) {
      this(serverUrl, appId, privateKey);
      this.format = format;
   }

   public DefaultAlipayClient(String serverUrl, String appId, String privateKey, String format, String charset) {
      this(serverUrl, appId, privateKey);
      this.format = format;
      this.charset = charset;
   }

   public DefaultAlipayClient(String serverUrl, String appId, String privateKey, String format, String charset, String alipayPulicKey) {
      this(serverUrl, appId, privateKey);
      this.format = format;
      this.charset = charset;
      this.alipayPublicKey = alipayPulicKey;
   }

   public AlipayResponse execute(AlipayRequest request) throws AlipayApiException {
      return this.execute(request, (String)null);
   }

   public AlipayResponse execute(AlipayRequest request, String accessToken) throws AlipayApiException {
      Object parser = null;
      if("xml".equals(this.format)) {
         parser = new ObjectXmlParser(request.getResponseClass());
      } else {
         parser = new ObjectJsonParser(request.getResponseClass());
      }

      return this._execute(request, (AlipayParser)parser, accessToken);
   }

   private AlipayResponse _execute(AlipayRequest request, AlipayParser parser, String authToken) throws AlipayApiException {
      Map rt = this.doPost(request, authToken);
      if(rt == null) {
         return null;
      } else {
         AlipayResponse tRsp = null;

         try {
            tRsp = parser.parse((String)rt.get("rsp"));
            tRsp.setBody((String)rt.get("rsp"));
            if(!StringUtils.isEmpty(this.alipayPublicKey)) {
               SignItem e = parser.getSignItem(request, tRsp);
               if(e == null) {
                  throw new AlipayApiException("sign check fail: Body is Empty!");
               }

               if(tRsp.isSuccess() || !tRsp.isSuccess() && !StringUtils.isEmpty(e.getSign())) {
                  boolean rsaCheckContent = AlipaySignature.rsaCheckContent(e.getSignSourceDate(), e.getSign(), this.alipayPublicKey, this.charset);
                  if(!rsaCheckContent) {
                     if(StringUtils.isEmpty(e.getSignSourceDate()) || !e.getSignSourceDate().contains("\\/")) {
                        throw new AlipayApiException("sign check fail: check Sign and Data Fail!");
                     }

                     String srouceData = e.getSignSourceDate().replace("\\/", "/");
                     boolean jsonCheck = AlipaySignature.rsaCheckContent(srouceData, e.getSign(), this.alipayPublicKey, this.charset);
                     if(!jsonCheck) {
                        throw new AlipayApiException("sign check fail: check Sign and Data Fail！JSON also！");
                     }
                  }
               }
            }
         } catch (RuntimeException var10) {
            AlipayLogger.logBizError((String)rt.get("rsp"));
            throw var10;
         } catch (AlipayApiException var11) {
            AlipayLogger.logBizError((String)rt.get("rsp"));
            throw new AlipayApiException(var11);
         }

         tRsp.setParams((AlipayHashMap)rt.get("textParams"));
         if(!tRsp.isSuccess()) {
            AlipayLogger.logErrorScene(rt, tRsp, "");
         }

         return tRsp;
      }
   }

   public Map doPost(AlipayRequest request, String accessToken) throws AlipayApiException {
      HashMap result = new HashMap();
      RequestParametersHolder requestHolder = new RequestParametersHolder();
      AlipayHashMap appParams = new AlipayHashMap(request.getTextParams());
      requestHolder.setApplicationParams(appParams);
      if(StringUtils.isEmpty(this.charset)) {
         this.charset = "UTF-8";
      }

      AlipayHashMap protocalMustParams = new AlipayHashMap();
      protocalMustParams.put("method", request.getApiMethodName());
      protocalMustParams.put("version", request.getApiVersion());
      protocalMustParams.put("app_id", this.appId);
      protocalMustParams.put("sign_type", this.sign_type);
      protocalMustParams.put("terminal_type", request.getTerminalType());
      protocalMustParams.put("terminal_info", request.getTerminalInfo());
      protocalMustParams.put("notify_url", request.getNotifyUrl());
      protocalMustParams.put("charset", this.charset);
      Long timestamp = Long.valueOf(System.currentTimeMillis());
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
      protocalMustParams.put("timestamp", df.format(new Date(timestamp.longValue())));
      requestHolder.setProtocalMustParams(protocalMustParams);
      AlipayHashMap protocalOptParams = new AlipayHashMap();
      protocalOptParams.put("format", this.format);
      protocalOptParams.put("auth_token", accessToken);
      protocalOptParams.put("alipay_sdk", "alipay-sdk-java-dynamicVersionNo");
      protocalOptParams.put("prod_code", request.getProdCode());
      requestHolder.setProtocalOptParams(protocalOptParams);
      if("RSA".equals(this.sign_type)) {
         String urlSb = AlipaySignature.getSignatureContent(requestHolder);
         protocalMustParams.put("sign", AlipaySignature.rsaSign(urlSb, this.privateKey, this.charset));
      } else {
         protocalMustParams.put("sign", "");
      }

      StringBuffer urlSb1 = new StringBuffer(this.serverUrl);

      String rsp;
      try {
         rsp = WebUtils.buildQuery(requestHolder.getProtocalMustParams(), this.charset);
         String e = WebUtils.buildQuery(requestHolder.getProtocalOptParams(), this.charset);
         urlSb1.append("?");
         urlSb1.append(rsp);
         if(e != null & e.length() > 0) {
            urlSb1.append("&");
            urlSb1.append(e);
         }
      } catch (IOException var15) {
         throw new AlipayApiException(var15);
      }

      rsp = null;

      try {
         if(request instanceof AlipayUploadRequest) {
            AlipayUploadRequest e1 = (AlipayUploadRequest)request;
            Map fileParams = AlipayUtils.cleanupMap(e1.getFileParams());
            rsp = WebUtils.doPost(urlSb1.toString(), appParams, fileParams, this.charset, this.connectTimeout, this.readTimeout);
         } else {
            rsp = WebUtils.doPost(urlSb1.toString(), (Map)appParams, this.charset, this.connectTimeout, this.readTimeout);
         }
      } catch (IOException var14) {
         throw new AlipayApiException(var14);
      }

      result.put("rsp", rsp);
      result.put("textParams", appParams);
      result.put("protocalMustParams", protocalMustParams);
      result.put("protocalOptParams", protocalOptParams);
      result.put("url", urlSb1.toString());
      return result;
   }

   static {
      Security.setProperty("jdk.certpath.disabledAlgorithms", "");
   }
}
